package CompletableFuture.常用方法;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;

/**
 * @program: algorithm
 * @description: thenApply、handle
 * @author: 陈恩涛
 * @create: 2022-08-09 23:12
 **/
public class CfApiDemo4 {
    public static void main(String[] args){

        final CompletableFuture<String> cf1 = CompletableFuture.supplyAsync(() -> {
            System.out.println("A is come in");
            try {
                TimeUnit.SECONDS.sleep(2);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return "playA";
        });

        final CompletableFuture<String> cf2 = CompletableFuture.supplyAsync(() -> {
            System.out.println("B is come in");
            try {
                TimeUnit.SECONDS.sleep(1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return "playB";
        });

        System.out.println(cf2.applyToEither(cf1, r -> "winner is " + r).join());
    }
}
